package com.fhyc.controller;

import com.fhyc.common.XPage;
import com.fhyc.dto.LoadCurveShowDto;
import com.fhyc.entity.SoInFhCompare;
import com.fhyc.service.SoInFhCompareService;
import com.fhyc.service.SoInLoadForecastService;
import com.fhyc.vo.ResponseResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/fm/loadForecast")
public class LoadForecastController {

    @Resource
    private SoInLoadForecastService soInLoadForecastService;

    @Resource
    private SoInFhCompareService soInFhCompareService;

    /**
     * 条件查询D+1 负荷预测
     *
     * @param dataDate
     * @param areaId
     * @param loadTypeId
     * @param teamIds
     * @return
     */
    @GetMapping("/queryLoadForecastD1")
    public ResponseResultVO<LoadCurveShowDto> queryLoadForecastD1(@RequestParam(required = false) String dataDate,
                                                                  @RequestParam(required = false) String areaId,
                                                                  @RequestParam(required = false) Integer loadTypeId,
                                                                  @RequestParam(required = false) List<Integer> teamIds) {
        LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate

        LoadCurveShowDto resp = soInLoadForecastService.queryLoadForecastD1(areaId, loadTypeId, localDate, teamIds);
        return new ResponseResultVO<>(resp);
    }

    /**
     * 条件查询D+10 负荷预测
     *
     * @param startDate
     * @param endDate
     * @param areaId
     * @param loadTypeId
     * @param teamIds
     * @return
     */
    @GetMapping("/queryLoadForecastD10")
    public ResponseResultVO<LoadCurveShowDto> queryLoadForecastD10(@RequestParam(required = false) String startDate,
                                                                   @RequestParam(required = false) String endDate,
                                                                   @RequestParam(required = false) String areaId,
                                                                   @RequestParam(required = false) Integer loadTypeId,
                                                                   @RequestParam(required = false) List<Integer> teamIds) {
        LocalDate startTime = LocalDate.parse(startDate); // 转换为 LocalDate
        LocalDate endTime = LocalDate.parse(endDate); // 转换为 LocalDate

        LoadCurveShowDto resp = soInLoadForecastService.queryLoadForecastD10(startTime, endTime, areaId, loadTypeId, teamIds);
        return new ResponseResultVO<>(resp);
    }

    /**
     * 日负荷对比数据导出
     *
     * @param response
     * @param dataDate
     * @param startDate
     * @param endDate
     * @param areaId
     * @param loadTypeId
     * @param teamIds
     * @param batch     批次
     */
    //Excel
    @GetMapping("/dailyLoadComparisonExportExcel")
    public void dailyLoadComparisonExportExcel(HttpServletResponse response,
                                               @RequestParam(required = false) String dataDate,
                                               @RequestParam(required = false) String startDate,
                                               @RequestParam(required = false) String endDate,
                                               @RequestParam(required = false) String areaId,
                                               @RequestParam(required = false) Integer loadTypeId,
                                               @RequestParam(required = false) List<Integer> teamIds,
                                               @RequestParam(required = false) String batch) {

        try {
            if (StringUtils.isNotEmpty(dataDate)) {
                LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate
                soInFhCompareService.dailyLoadComparison1ExportExcel(response, localDate, areaId, loadTypeId, teamIds, batch);
            }
            if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
                LocalDate startTime = LocalDate.parse(startDate); // 转换为 LocalDate
                LocalDate endTime = LocalDate.parse(endDate); // 转换为 LocalDate
                soInFhCompareService.dailyLoadComparison10ExportExcel(response, startTime, endTime, areaId, loadTypeId, teamIds, batch);
            }
        } catch (Exception e) {
            log.error(e.toString());
            throw new RuntimeException(e);
        }
    }

    /**
     * 日负荷对比
     *
     * @param pageNum
     * @param pageSize
     * @param dataDate
     * @param startDate
     * @param endDate
     * @param areaId
     * @param loadTypeId
     * @param teamIds
     * @param batch
     * @return
     */
    @GetMapping("/dailyLoadComparison")
    public ResponseResultVO<XPage<SoInFhCompare>> dailyLoadComparison(@RequestParam(defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                                                      @RequestParam(required = false) String dataDate,
                                                                      @RequestParam(required = false) String startDate,
                                                                      @RequestParam(required = false) String endDate,
                                                                      @RequestParam(required = false) String areaId,
                                                                      @RequestParam(required = false) Integer loadTypeId,
                                                                      @RequestParam(required = false) List<Integer> teamIds,
                                                                      @RequestParam(required = false) String batch) {

        XPage<SoInFhCompare> resp = null;
        if (StringUtils.isNotEmpty(dataDate)) {
            LocalDate localDate = LocalDate.parse(dataDate); // 转换为 LocalDate
            resp = soInFhCompareService.dailyLoadComparison1(pageNum, pageSize, localDate, areaId, loadTypeId, teamIds, batch);
        }
        if (StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate)) {
            LocalDate startTime = LocalDate.parse(startDate); // 转换为 LocalDate
            LocalDate endTime = LocalDate.parse(endDate); // 转换为 LocalDate
            resp = soInFhCompareService.dailyLoadComparison10(pageNum, pageSize, startTime, endTime, areaId, loadTypeId, teamIds, batch);
        }
        return new ResponseResultVO<>(resp);
    }

}
